<?php
class Admin_GroupuserController extends Zend_Controller_Action
{

    public function init()
    {
		$layoutPath = APPLICATION_PATH . '/templates/admin';
		$options = array (
				'layout' => 'index',
				'layoutPath' => $layoutPath 
		);
		Zend_Layout::startMvc ( $options );
    }

    public function indexAction()
    {
		$Condition = array ();
		$this->view->GroupUsers = UserGroup::getAllWithoutPager ( $Condition );
    }

    public function createAction()
    {
		$requests = $this->getRequest ()->getParams ();
		$this->view->Options=Permission::createOption();
		if ($this->getRequest ()->isPost ()) {
			try {
				$GroupUser = new UserGroup();
				$GroupUser->merge($requests);
				$GroupUser->created=date('Y-m-d H:i:s',time());
				$GroupUser->created_by=1;
				if($GroupUser->trySave()){
					if($_FILES){
						$uploadDir = PUBLIC_PATH."/uploads/groupusers/";
						$image=self::_uploadFiles($uploadDir,$GroupUser->id);
						$GroupUser->avatar=$image;
						$this->view->Message='Create group user successfull';
						$GroupUser->save();
					}
					$DeleteAccess = UserGroupPermission::getAllWithoutPager(array('group_id=?'=>$GroupUser->id));
					foreach ($DeleteAccess as $delete){
						$delete->delete();
					}
					$ListAccess = $requests['listaccess'];
					foreach ($ListAccess as $list){
						$Access = new UserGroupPermission();
						$Access->permission_id=$list;
						$Access->group_id=$GroupUser->id;
						$Access->save();
					}
				}
			} catch (Exception $e) {
				print_r($e->getMessage());die();
			}
		}else{
			unset($requests['id']);
		}
		
    }

    private static function _uploadFiles($uploadDir = '', $FileName = '')
    {
		if (! is_dir ( $uploadDir )) {
			@mkdir ( $uploadDir );
			chmod ( $uploadDir, 0777 );
		}
		// upload thumb image
		$image = new Zend_Form_Element_File ( 'image' );
		$image->setDestination ( $uploadDir );
		$image->addValidator ( 'Count', false, array (
				'min' => 1,
				'max' => 1 
		) );
		$image->addValidator ( 'Extension', false, 'jpg,png,gif' );
		$image->addFilter ( 'Rename', array (
				'target' => $uploadDir. $FileName.'.jpg',
				'overwrite' => true 
		) );
		$image->receive ();
		return $image->getFileName ( null, false );
    }

    private static function _deleteFile($uploadDir = '')
    {
		if (is_dir ( $uploadDir )) {
			unlink ( $uploadDir );
		}
    }

    public function editAction()
    {
        $requests = $this->getRequest()->getParams();
        $GroupUser = UserGroup::getById($requests['id']);
        if($GroupUser){
        	$this->view->GroupUser = $GroupUser;
        	$this->view->Options=Permission::createOption();
        	
        	if($this->getRequest()->isPost()){
	        	try {
					$GroupUser->merge($requests);
					$GroupUser->modified=date('Y-m-d H:i:s',time());
					$GroupUser->modified_by=1;
					if($GroupUser->trySave()){
						unset($requests['id']);
						if($_FILES){
							$uploadDir = PUBLIC_PATH."/uploads/groupusers/";
							self::_deleteFile($GroupUser->getThumbImage());
							$image=self::_uploadFiles($uploadDir,$GroupUser->id);
							$GroupUser->avatar=$image;
							$GroupUser->save();
							$this->view->Message='Update group user successfull';
						}
					}
					$DeleteAccess = UserGroupPermission::getAllWithoutPager(array('group_id=?'=>$GroupUser->id));
					foreach ($DeleteAccess as $delete){
						$delete->delete();
					}
					if($requests['listaccess']!=''){
					$ListAccess = $requests['listaccess'];
						foreach ($ListAccess as $list){
							$Access = new UserGroupPermission();
							$Access->merge($requests);
							$Access->permission_id=$list;
							$Access->group_id=$GroupUser->id;
							$Access->save();
						}
					}
				} catch (Exception $e) {
					print_r($e->getMessage());die();
				}
        	}
        }else{
        	$this->_redirect('/admin');
        }
    }

    public function deleteAction()
    {
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender (true);
    	$json = array('STATUS'=>0);
        try {
        	$requests = $this->getRequest()->getParams();
        	$GroupUser =UserGroup::getById($requests['id']);
        	$GroupUser->delete();
        	$json['STATUS']=1;
        } catch (Exception $e) {
        	$json['STATUS']=0;
        }
        header ( 'Content-type: application/json' );
        echo json_encode ( $json );
    }
    public function deletemultiAction()
    {
    	$this->_helper->layout->disableLayout ();
    	$this->_helper->viewRenderer->setNoRender (true);
    	
    	try {
    		$requests = $this->getRequest()->getParams();
    		
    		$arrID=$requests['groupuser'];
    		foreach ($arrID as $key=>$value){
    			
    			$GroupUser=UserGroup::getById($value);
    			
    			if($GroupUser){
    				$GroupUser->delete();
    			}
    		}
    		$this->_redirect ( '/admin/groupuser/index');
    	} catch (Exception $e) {
    		print_r($e->getMessage());die();	
    	}
    }


}







